package de.baumann.browser.utils

import android.text.TextUtils
import de.baumann.browser.OdinApplication
import de.baumann.browser.R
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.db.ETHWallet

fun saveUser(loginInfo: LoginInfo) {
    putObject("user", loginInfo)
}

fun getUser(): LoginInfo? {
    return getObject("user")
}

fun saveETHWallet(eTHWallet: ETHWallet) {
    putObject("eTHWallet", eTHWallet)
}

fun getETHWallet(): ETHWallet? {
    return getObject("eTHWallet")
}

fun saveMoneyAddress(str: String) {
    put("MoneyAddress", str)

}

fun getMoneyAddress(): String? {
    return getString("MoneyAddress", "")
}

fun saveWalletName(walletName: String) {
    put("WalletName", walletName)
}

fun saveIntent(intents: String) {
    put("intents", intents)
}

fun gaveIntent(): String? {
    return getString("intents", "")
}

fun saveInviteCode(inviteCode: String) {
    put("inviteCode", inviteCode)
}

fun saveInviteCodeStatus(inviteCodeStatus: Int) {
    put("inviteCodeStatus", inviteCodeStatus)
}

fun getInviteCodeStatus(): Int {
    return getInt("inviteCodeStatus", 0)
}

fun getInviteCode(): String? {
    return getString("inviteCode", "")
}

fun saveFormAddress(ethAddress: String) {
    put("fromAddress", ethAddress)
}

fun getFormAddress(): String? {
    return getString("fromAddress", "")
}


fun getWalletName(): String? {
    return getString("WalletName", "")
}

fun isLogined(): Boolean {
    return getUser() != null
}

fun getIsLogin(): Boolean {
    return getBoolean("isLogin")
}

fun saveLogin(isLogin: Boolean) {
    return put("isLogin", isLogin)
}

fun save(isfirst: Boolean) {
    put("isfirst", isfirst)
}


fun isFirst(): Boolean {
    return getBoolean("isfirst")
}

fun getUserId(): String {
    return if (getUser() == null) {
        ""
    } else {
        getUser()!!.user_id.toString()
    }
}

fun getUserName(): String? {
    val loginInfo: LoginInfo = getUser()
            ?: return OdinApplication.getInstance().getString(R.string.login_now)

    return if (!TextUtils.isEmpty(loginInfo.user_name)) {
        loginInfo.user_name
    } else {
        loginInfo.loginName
    }
}

fun getPoolName(): String {
    val loginInfo: LoginInfo = getUser() ?: return ""

    return if (!TextUtils.isEmpty(loginInfo.poolname)) {
        loginInfo.poolname
    } else {
        ""
    }
}

fun updateUserName(name: String) {
    val loginInfo = getUser()
    loginInfo?.user_name = name
    saveUser(loginInfo!!)
}

fun updateLoginname(name: String) {
    val loginInfo = getUser()
    loginInfo?.loginName = name
    saveUser(loginInfo!!)
}

fun updateUserPhone(phone: String) {
    val loginInfo = getUser()
    loginInfo?.phone = phone
    saveUser(loginInfo!!)
}

fun updateUserETHUrl(ethUrl: String) {
    val loginInfo = getUser()
    loginInfo?.eth_address = ethUrl
    saveUser(loginInfo!!)
}

fun removeUser() {
    remove("user")
}

fun getShareContent(): String {
//    return getUser()!!.invite_url
    return "邀请链接"
}

fun getPhoneStatus(): Int {
    if (getUser() == null) {
        return 1
    }
    return getUser()!!.phoneStatus
}

fun updatePhoneStatus() {
    val user = getUser()
    user!!.phoneStatus = 1
    saveUser(loginInfo = user)
}

fun getLicenceStatus(): Int {
    val user = getUser()
    return user?.lisenceStatus ?: 0
}

fun updateLicenceStatus(licenceStatus: Int) {
    val localUser = getUser()
    localUser!!.lisenceStatus = licenceStatus
    saveUser(loginInfo = localUser)
}

fun updateLicence(licence: String) {
    val user = getUser()
    user!!.licence = licence
    saveUser(loginInfo = user)
}

fun getLicence(): String {
    val user = getUser()
    if (user != null && user.licence != null && user.licence.isNotEmpty()) {
        return user.licence
    }
    return ""

}

fun updateHead(headUrl: String) {
    val user = getUser()
    user!!.headUrl = headUrl
    saveUser(loginInfo = user)
}

fun getUserHead(): String? {
    if (getUser() != null) {
        return getUser()!!.headUrl
    }
    return ""
}

fun updateQQ(qq: String) {
    val user = getUser()
    user!!.qq = qq
    saveUser(loginInfo = user)
}

fun getNodeName(): String {
    val user = getUser()
    if (user != null && user.nodeName != null) {
        return user.nodeName
    }
    return ""
}


fun getToken(): String {
    val user = getUser()
    return if (user == null) {
        ""
    } else {
        user.token
    }
}






